﻿package core.drawing
{
    import core.enum.*;
    import core.image.*;
    import flash.events.*;
    import flash.geom.*;

    final public class CropTool extends Tool
    {
        var pr:Point;
        var po:Point;
        var rect:Rectangle;
        var ax:int;
        var ay:int;

        public function CropTool(param1:Tuyitu)
        {
            super(param1, ToolType.Crop, false);
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            this.UpdateReferences();
            ed.cursor.freeze = true;
            ed.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false, 1, true);
            ed.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
            this.rect = new Rectangle();
            if (options.constraint != "none")
            {
                this.ax = options.width;
                this.ay = options.height;
            }
            else
            {
                this.ax = 0;
                this.ay = 0;
            }
            this.pr = canvas.GetRealMousePosition();
            if (this.pr.x < 0)
            {
                this.pr.x = 0;
            }
            else if (this.pr.x >= canvas.coating.width)
            {
                this.pr.x = canvas.coating.width - 1;
            }
            if (this.pr.y < 0)
            {
                this.pr.y = 0;
            }
            else if (this.pr.y >= canvas.coating.height)
            {
                this.pr.y = canvas.coating.height - 1;
            }
            canvas.DeselectAll();
            return;
        }// end function

        override public function MouseDrag(event:MouseEvent) : void
        {
            this.po = canvas.GetRealMousePosition();
            if (this.po.x < 0)
            {
                this.po.x = 0;
            }
            else if (this.po.x >= canvas.coating.width)
            {
                this.po.x = canvas.coating.width - 1;
            }
            if (this.po.y < 0)
            {
                this.po.y = 0;
            }
            else if (this.po.y >= canvas.coating.height)
            {
                this.po.y = canvas.coating.height - 1;
            }
            this.rect = Misc.CalculateRectangle(this.pr, this.po, this.ax, this.ay);
            canvas.coating.DrawRect(this.rect);
            ed.navigator.SetSelectionSize(Math.round(this.rect.width / canvas.zoomlevel), Math.round(this.rect.height / canvas.zoomlevel));
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            ed.cursor.freeze = false;
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            canvas.coating.Clear();
            if (this.rect.width > 8 && this.rect.height > 8)
            {
                ed.cm.CropOverlay(this.rect, options);
            }
            else
            {
                ed.navigator.SetSelectionSize(0, 0);
            }
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

    }
}
